use Action;
use Actions;
use CommandActions;
use ActionStatus;
use Component;

package ActionManager;


@ISA = qw/Component/;

sub new{
    my $class = shift;
    my $self = {};

    bless $self, $class;
    $self->init(@_);

    return $self;
} # new

sub init{
    my $self = shift;

    $self->dependencies(qw/DependencyResolver LogManager/);
    $self->{ACTIONS} = [];
} # init


sub pop{
    my $self = shift;
    return shift(@{$self->{ACTIONS}});
}

sub push{
    my $self = shift;
    my $action = shift;

    return push(@{$self->{ACTIONS}}, $action);
}

sub actions{
    my $self = shift;

    my @actions = @{$self->{ACTIONS}};
    return @actions;
}

sub newAction{
    my $self = shift;
    my $type = shift;

  

    # Explicitly passed parameters
    my $parameters = shift;

    # Parameters encoded in the name of the action
    my @type_split = split /\|/, $type;
    $type = shift(@type_split)."Action";

    foreach my $parameter (@type_split){
	## Parameters can have the form NAME=VALUE
	my @parameter_split = split /=/, $parameter;
	my $paramName = $parameter_split[0];

	# Value is optional, it defaults to "1"
	my $paramValue = 1;
	$paramValue = $parameter_split[1] if (scalar(@parameter_split) > 1);
	
	# Add to the explicitly passed parameters
	$parameters->{$paramName} = $paramValue;
    }

#    my $lm = $self->{LogManager};    
#    $lm->log("Instancing Action: $type->new");

    my $action = eval "$type->new";
    $action->params($parameters);

    $self->{DependencyResolver}->resolveDependencies($action);
    return $action;
}

1;
